package com.lfc.flowable.controller;

import com.lfc.flowable.domian.AjaxResult;
import com.lfc.flowable.domian.dto.TaskInfo;
import com.lfc.flowable.service.FlowableService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.flowable.engine.ProcessEngine;
import org.springframework.web.bind.annotation.*;

/**
 * 流程控制类
 * 已发任务，代办任务，全部任务
 */
@RestController
@RequestMapping("/flow")
public class FlowableController {

    @Resource
    private ProcessEngine processEngine;

    @Resource
    FlowableService flowableService;

    /**
     * 我的待办
     *
     * @param param
     * @return
     */
    @PostMapping("/mylist")
    public AjaxResult myList(@RequestBody TaskInfo param) {

        return AjaxResult.success(flowableService.myList(param));
    }

    /**
     * 任务办理时间轴
     *
     * @param taskId
     * @return
     */
    @GetMapping(value = "/history/taskId")
    public AjaxResult history(@RequestParam String taskId) {
        return AjaxResult.success(flowableService.history(taskId));
    }

    /**
     * 任务办理时间轴
     *
     * @param processInstanceId
     * @return
     */
    @GetMapping(value = "/history/processInstanceId")
    public AjaxResult historyByProcessInstanceId(@RequestParam String processInstanceId) {
        return AjaxResult.success(flowableService.historyByProcessInstanceId(processInstanceId));
    }


    /**
     * 生成流程图
     *
     * @param processInstanceId 任务ID
     */
    @GetMapping("/processDiagram")
    public void genProcessDiagram(HttpServletResponse httpServletResponse, String processInstanceId) throws Exception {
        flowableService.genProcessDiagram(httpServletResponse, processInstanceId);
    }

    /**
     * 查询所有流程实例列表-包含在运行和已结束
     *
     * @param businessKey
     * @param name
     * @param pageSize
     * @param pageNum
     * @return
     */
    @PostMapping(value = "/listHistoryProcess")
    public AjaxResult listHistoryProcess(@RequestParam(required = false) String businessKey,
                                         @RequestParam(required = false) String name,
                                         Integer pageSize, Integer pageNum) {
        return AjaxResult.success(flowableService.listHistoryProcess(businessKey, name, pageSize, pageNum));
    }
}
